package ms.jasim.console.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import ms.jacrim.framework.Configuration;
import ms.jasim.console.gui.component.DialogHeaderPanel;
import ms.jasim.console.gui.component.IContextualComponent;
import ms.jasim.console.gui.component.ME_GoalPanel;
import ms.jasim.console.gui.component.ME_RelationshipPanel;
import ms.jasim.console.gui.component.ME_RolePanel;
import ms.jasim.framework.DefaultEventTypeProvider;
import ms.jasim.framework.PddlModel;
import ms.spm.IAppContext;
import ms.spm.implement.SimpleAppContext;

public class ModelEditor extends JFrame {

	private static final long serialVersionUID = 1L;

	/**
	 * Launch the application
	 * 
	 * @param args
	 * @throws UnsupportedLookAndFeelException
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws ClassNotFoundException
	 */
	public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
			UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IAppContext context = SimpleAppContext.createRootContext();
					Configuration config = context.getService(Configuration.class);
					String modelFile = config.get("model_file");
					PddlModel model = new PddlModel();
					model.loadFromFile(modelFile);
					if (model != null)
						context.addService(model);
					context.addService(DefaultEventTypeProvider.createStdEventSet());
					
					ModelEditor frame = new ModelEditor();
					frame.setContext(context);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	private IAppContext context;
	private ArrayList<IContextualComponent> tabs;


	/**
	 * Create the frame
	 */
	public ModelEditor() {
		super();
		setBounds(100, 100, 700, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//
		this.setContentPane(createContent(new JPanel(new BorderLayout())));
	}

	protected JPanel createContent(JPanel panel) {
		
		tabs = new ArrayList<IContextualComponent>();
		
		final DialogHeaderPanel dialogHeaderPanel = new DialogHeaderPanel();
		dialogHeaderPanel.setSubTitle("");
		dialogHeaderPanel.setTitle("Model Editor");
		
		panel.add(dialogHeaderPanel, BorderLayout.NORTH);
		
		JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.addTab("Role", registerSubPanel(new ME_RolePanel()));
		tabbedPane.addTab("Goal", registerSubPanel(new ME_GoalPanel()));
		tabbedPane.addTab("Relationship", registerSubPanel(new ME_RelationshipPanel()));
		
		panel.add(tabbedPane, BorderLayout.CENTER);
		
		return panel;
	}

	private Component registerSubPanel(Component panel) {
		tabs.add((IContextualComponent) panel);
		return panel;
	}

	protected void setContext(IAppContext context) {
		this.context = context.createChildContext("$ModelEditor");
		for (IContextualComponent comp : tabs)
			comp.setContext(this.context);
	}
}
